import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class student_manage {
	List<student> studentList = new ArrayList<>();
	String filename ="student.txt";
	private Scanner sc = new Scanner(System.in);
	
	public void showAllStudent() {
		if (studentList.isEmpty()) {
			System.out.println("暂无学生信息！");
			System.out.println("按任意键继续");
			sc.next();
			return;
		}
		for (student s : studentList) {
			s.show();
		}
		System.out.println("以上为所有学生信息，按任意键继续");
		sc.next();
	}
	
	public boolean isStudentExist(String studentId) {

		boolean flag = false;
		for (student s : studentList) {
			// 字符串判断是否相同 使用equals()
			if (s.getId().equals(studentId)) {
				flag = true;
				break;
			}
		}
		return flag;
	}
	
    //添加学生
	public void addStudent() {
		String input;
		student s = new student();
		System.out.println("====新增学生信息=====");
		System.out.println("请输入学号：");
		do {
			input = sc.nextLine();
		}while(input.length() ==0 );
		s.setId(input);

		System.out.println("请输入姓名：");
		do {
			input = sc.nextLine();
		}while(input.length() ==0 );
		s.setName(input);

		System.out.println("请输入性别：");
		do {
			input = sc.nextLine();
		}while(input.length() ==0 );
		s.setGender(input);

		int in_age;
		System.out.println("请输入年龄：");
		do {
			in_age = sc.nextInt();
		}while(in_age < 0);
		s.setAge(in_age);
		
		if (isStudentExist(s.getId())) {
			System.out.println("添加的学生已经存在！");
			System.out.println("按任意键继续");
			sc.next();
			return;
		} else {			
			studentList.add(s);
		}
		System.out.println("添加成功！");
		System.out.println("按任意键继续");
		sc.next();
	}

    public void deleteStudent() {
        String input;
        System.out.println("请输入要删除的学生学号：【输入-1退出】");
        do {
            input = sc.nextLine();
        }while(input.length() ==0 );
        if (input.equals("-1")) {
            return;
        }
        for (student s : studentList) {
            if (s.getId().equals(input)) {
				s.show();
				System.out.println("确认删除该学生吗?【Y/N】");
				String confirm = sc.next();
				if (!confirm.equals("Y")) {
					System.out.println("取消删除！");
					System.out.println("按任意键继续");
					sc.next();
					return;
				}
                studentList.remove(s);
                System.out.println("删除成功！");
				System.out.println("按任意键继续");
				sc.next();
                return;
            }
        }
        System.out.println("删除失败，请重新输入！");
        deleteStudent();
    }

    public void updateStudent() {
		String input;
		System.out.println("请输入要修改的学生学号：【输入-1退出】");
		do {
			input = sc.nextLine();
		}while(input.length() ==0 );
		if (input.equals("-1")) {
			return;
		}
		for (student s : studentList) {
			if (s.getId().equals(input)) {
				System.out.println("请输入修改后的姓名：");
				do {
					input = sc.nextLine();
				}while(input.length() ==0 );
				s.setName(input);

				System.out.println("请输入修改后的性别：");
				do {
					input = sc.nextLine();
				}while(input.length() ==0 );
				s.setGender(input);

				int in_age;
				System.out.println("请输入修改后的年龄：");
				do {
					in_age = sc.nextInt();
				}while(in_age < 0);
				s.setAge(in_age);
				System.out.println("修改成功！");
				System.out.println("按任意键继续");
				sc.next();
				return;
			}
		}
		System.out.println("修改失败，请重新输入！");
		updateStudent();
        
    }



    public void searchStudent() {
		String input;
		System.out.println("请输入要搜索的学生学号：【输入-1退出】");
		do {
			input = sc.nextLine();
		}while(input.length() ==0 );
		if (input.equals("-1")) {
			return;
		}
		for (student s : studentList) {
			if (s.getId().equals(input)) {
				s.show();
				System.out.println("按任意键继续");
				sc.next();
				return;
			}
		}
		System.out.println("搜索失败，请重新输入！");
		searchStudent();
        
    }

	//保存到文件
	public void saveToFile() {
		 // 序列化 student 对象到文件
        try {
            FileOutputStream fileOut = new FileOutputStream(filename);
            ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
            
         
           	objectOut.writeObject(studentList);

            objectOut.close();
            fileOut.close();

            System.out.println("student object has been serialized and saved to student.txt");
			System.out.println("按任意键继续");
			sc.next();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	public void readFromFile() {
		 // 反序列化并读取文件中的 student 对象
        try {
            FileInputStream fileIn = new FileInputStream(filename);
            ObjectInputStream objectIn = new ObjectInputStream(fileIn);
            
            //清空List
            studentList.clear();
            studentList = (List<student>) objectIn.readObject();

            objectIn.close();
            fileIn.close();

            // 打印反序列化后的 student 对象信息
            System.out.println("Deserialized student:");
            for(student s:studentList) {
            	s.show();
            }
			System.out.println("按任意键继续");
			sc.next();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
}
